package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.Result;
import com.hl.sfzy.entity.SysUser;
import com.hl.sfzy.exception.AppException;
import com.hl.sfzy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @program: sfzy
 * @ClassName SysUserController
 * @description:
 * @author: lyp
 * @create: 2024-07-10 14:24
 * @Version 1.0
 **/
@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;



    /*
    * 新增
    * */
    @PostMapping("/sysUser")
    public Result add(@RequestBody SysUser sysUser){
        int num = sysUserService.add(sysUser);
        return num > 0 ? Result.success() : Result.error();
    }

    /*
    * 删除
    * */
    @DeleteMapping("/sysUser/{id}")
    public Result remove(@PathVariable("id") Integer id){
        int num = sysUserService.remove(id);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/sysUser")
    public Result removeBatch(Integer[] ids){
        int num = sysUserService.removeBatch(ids);
        return num > 0 ? Result.success() : Result.error();
    }

    @PutMapping("/sysUser")
    public Result edit(@RequestBody SysUser sysUser){
        System.out.println(sysUser);
        int num = sysUserService.edit(sysUser);
        return num > 0 ? Result.success() : Result.error();
    }

    @GetMapping("/sysUser/{id}")
    public Result findById(@PathVariable("id") Integer id){
        SysUser sysUser = sysUserService.findById(id);
        return Result.success(sysUser);
    }

    @GetMapping("/sysUser")
    public Result findAll(SysUser sysUser){
        List<SysUser> list = sysUserService.find(sysUser);
        if(list.size()>0){
            return Result.success(list);
        }
        throw new AppException("402");
    }

    @GetMapping("/sysUser/page")
    public Result findPage(
            @RequestParam(value = CommonConstant.PAGE_NUM,required = false,defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(value = CommonConstant.PAGE_SIZE,required = false,defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            SysUser sysUser) {
        Map<String, Object> map = sysUserService.findPage(pageNum, pageSize, sysUser);
        return Result.success(map);
    }

    @GetMapping("/sysUser/byName/{username}")
    public Result findByUsername(@PathVariable("username") String username){
        SysUser sysUser = sysUserService.findByUsername(username);
        return Result.success(sysUser);
    }

}

